<?php
declare (strict_types = 1);

namespace app\index\controller;

use app\model\questionnaireModel;
use app\model\scoreModel;
use app\model\usersModel;
use think\facade\Request;
use think\facade\Validate;
use think\facade\View;

class Index
{
    public function home()
    {
        $quesList = questionnaireModel::select();
        View::assign('quesList',$quesList);
        return View::fetch('index/home');
    }
    public function index()
    {
        $question_id = Request::param('question_id');
        $validate = Validate::rule([
            'question_id|问卷id' => 'require|between:1,'.PHP_INT_MAX,
        ]);

        if(!$validate->check(['question_id'=>$question_id])){
            echo $validate->getError();
            exit();
        }
        $question = questionnaireModel::where('question_id','=',$question_id)->find();

        View::assign('question',$question);
        return View::fetch('index/index');
    }

    public function add()
    {
        return View::fetch();
    }
    public function addSave()
    {
        $params = Request::param();
        $validate = Validate::rule([
            'title|问卷主题' => 'require|min:1|max:255',
            'explain|问卷说明' => 'require|min:1|max:255',
            'ques_one|选项1' => 'require|min:1|max:50',
            'ques_two|选项2' => 'require|min:1|max:50',
            'ques_three|选项3' => 'require|min:1|max:50',
            'ques_four|选项4' => 'require|min:1|max:50',
            'ques_five|选项5' => 'require|min:1|max:50',
        ]);

        if(!$validate->check($params)){
            echo $validate->getError();
            exit();
        }
        $params["add_time"] = time();
        $params["update_time"] = time();

        $result = questionnaireModel::create($params);

        if ($result) {
            echo "<script>alert('增加成功');</script>";
            echo "<script>location.href='/index.php?s=index/index/home'</script>";
        } else {
            echo "<script>alert('增加失败,请联系管理员：1292888678@qq.com');</script>";
        }

    }

    public function grade()
    {
        session_start();
        $params = Request::param();
        $validate = Validate::rule([
            'title_id|问卷主题id'=>'require|between:1,'.PHP_INT_MAX,
            'ques_one|选项1' => 'require|min:1|max:5',
            'ques_two|选项2' => 'require|min:1|max:5',
            'ques_three|选项3' => 'require|min:1|max:5',
            'ques_four|选项4' => 'require|min:1|max:5',
            'ques_five|选项5' => 'require|min:1|max:5',
        ]);

        if(!$validate->check($params)){
            echo $validate->getError();
            exit();
        }
        $params['user_id'] = $_SESSION['id'];
        $params["add_time"] = time();
        $params["update_time"] = time();

        $result = scoreModel::create($params);

        if ($result) {
            echo "<script>alert('评分成功');</script>";
            echo "<script>location.href='/index.php?s=index/index/home'</script>";
        } else {
            echo "<script>alert('评分失败,请联系管理员：1292888678@qq.com');</script>";
        }
    }
    //用户编辑信息的链接
    public function edit()
    {
        $userEmail = Request::param('user_email');
        $validate = Validate::rule([
            'user_email'=>'require',
        ]);

        if(!$validate->check(['user_email'=>$userEmail])){
            echo $validate->getError();
            exit();
        }
        $user = usersModel::where('user_email','=',$userEmail)->find();
        View::assign('user',$user);
        return View::fetch();
    }

    public function editSave()
    {
        $params = Request::param();
        $validate = Validate::rule([
            'user_id|用户id' => 'require|between:1,'.PHP_INT_MAX,
            'user_name|用户名' => 'require|min:1|max:6',
            'user_email|邮箱' => 'require|min:2|max:100',
            'user_pwd|密码' => 'require|min:1|max:6',
            'pwds|确认密码' => 'require|min:1|max:6',
        ]);

        if(!$validate->check($params)){
            echo $validate->getError();
            exit();
        }
        if($params['user_pwd'] != $params['pwds']){
            echo '两次密码不一致,请重新输入';
            echo '<a href="javascript:void(0)" onclick="history.back();">返回上一页</a>';
            exit();
        }
        $params["update_time"] = time();

        $result = usersModel::update($params);

        if ($result) {
            echo "<script>alert('编辑成功,正在审核，请重新登录！');</script>";
            echo "<script>location.href='/index.php?s=index/index/home'</script>";
        } else {
            echo "<script>alert('编辑失败,请联系管理员：1292888678@qq.com');</script>";
        }
    }
    //
    public function scoreDetails()
    {
        $scoreList = scoreModel::select();
        foreach ($scoreList as &$value){
            $title_id = questionnaireModel::find($value['title_id']);
            $value['ques_title'] = $title_id['title'];
            $user_id = usersModel::find($value['user_id']);
            $value['user_name'] = $user_id['user_name'];
        }
        unset($value);
        View::assign('scoreList',$scoreList);
        return View::fetch("index/scoreDetails");
    }

    public function scoreEdit()
    {
        $score_id = Request::param('score_id');
        $validate = Validate::rule([
            'score_id|分数id' => 'require|between:1,'.PHP_INT_MAX,
        ]);

        if(!$validate->check(['score_id'=>$score_id])){
            echo $validate->getError();
            exit();
        }
        $scoreList = scoreModel::where('score_id','=',$score_id)->find();

        View::assign('scoreList',$scoreList);
        return View::fetch("index/scoreEdit");
    }
    public function scoreEditSave()
    {
        $params = Request::param();
        $validate = Validate::rule([
            'score_id|分数id' => 'require|between:1,'.PHP_INT_MAX,
            'title_id|问卷主题id' => 'require',
            'user_id|用户id' => 'require',
            'ques_one|选项1' => 'require',
            'ques_two|选项2' => 'require',
            'ques_three|选项3' => 'require',
            'ques_four|选项4' => 'require',
            'ques_five|选项5' => 'require',
        ]);

        if(!$validate->check($params)){
            echo $validate->getError();
            exit();
        }

        $params["update_time"] = time();

        $result = scoreModel::update($params);

        if ($result) {
            echo "<script>alert('编辑成功');</script>";
            echo "<script>location.href='/index.php?s=index/index/scoreDetails'</script>";
        } else {
            echo "<script>alert('编辑失败,请联系管理员：1292888678@qq.com');</script>";
        }
    }

    public function delete()
    {
        $score_id = Request::param('score_id');
        $validate = Validate::rule([
            'score_id|分数id' => 'require|between:1,'.PHP_INT_MAX,
        ]);

        if(!$validate->check(['score_id'=>$score_id])){
            echo $validate->getError();
            exit();
        }
        $result = scoreModel::destroy($score_id);

        if ($result) {
            echo "<script>alert('删除成功');</script>";
            echo "<script>location.href='/index.php?s=index/index/home'</script>";
        } else {
            echo "<script>alert('删除失败,请联系管理员：1292888678@qq.com');</script>";
        }
    }
}
